gqrs detection


In [1]:
import wfdb
import numpy as np
import matplotlib.pyplot as plt
import sys
sys.path.insert(0, '/home/scidb/HeartRatePatterns/Python')

#QRSDetectorOffline.py
from QRSDetectorOffline import QRSDetectorOffline

In [2]:
folder = "p05/p050140"
waveform = "p050140-2188-07-26-05-51"
sig, fields = wfdb.srdsamp(waveform,pbdir='mimic3wdb/matched/'+folder, sampto=10000)

In [3]:
print("signame: " + str(fields['signame']))
print("units: " + str(fields['units']))
print("fs: " + str(fields['fs']))
print("comments: " + str(fields['comments']))
print("fields: " + str(fields))


signame: ['aVR', 'II', 'I', 'III', 'ABP', 'CVP', 'PLETH']
units: ['mV', 'mV', 'mV', 'mV', 'mmHg', 'mmHg', 'NU']
fs: 125
comments: ['Location: micu']
fields: {'signame': ['aVR', 'II', 'I', 'III', 'ABP', 'CVP', 'PLETH'], 'fs': 125, 'units': ['mV', 'mV', 'mV', 'mV', 'mmHg', 'mmHg', 'NU'], 'comments': ['Location: micu']}

In [4]:
signalII = None
try:
    signalII = fields['signame'].index("II")
except ValueError:
    print("List does not contain value")
if(signalII!=None):
    print("List contain value")


List contain value

In [5]:
#array = wfdb.processing.normalize(x=sig[:, signalII], lb=-2, ub=2)
array = sig[:, signalII]
array = array[~np.isnan(sig[:, signalII])]
arrayNun = np.trim_zeros(array)
array = np.nan_to_num(array)

Save the Frame Secuence


In [6]:
fs = fields['fs']
fs


Out[6]:
125

Create Numpy Array


In [7]:
npArray = np.array(array)
npArray


Out[7]:
array([ 0.51968504,  0.48818898,  0.45669291, ...,  0.31496063,
        0.32283465,  0.33070866])

check sig:


In [8]:
plt.plot(npArray)
plt.show()



In [9]:
len(np.array(npArray))


Out[9]:
6565

In [10]:
sha = np.arange(npArray.shape[0])

In [11]:
test = np.stack((sha, np.nan_to_num(npArray))).T

In [12]:
ecg_data_path = 'ecg_data/'+waveform+'.csv'
np.savetxt(ecg_data_path, test, delimiter=',')
qrs_detector = QRSDetectorOffline(ecg_data_path=ecg_data_path, verbose=True,log_data=True, plot_data=True, 
                                  show_plot=True,signal_frequency=fs)


qrs peaks indices
[ 423  494  560  628  764  833  901  969 1040 1105 1174 1242 1313 1380 1449
 1514 1583 1651 1720 1790 1857 1928 2134 2203 2273 2345 2411 2551 2619 2689
 2758 2828 2898 2971 3037 3107 3245 3315 3384 3594 3666 3731 3869 3940 4142
 4211 4348 4420 4557 4694 4760 4898 4970 5108 5315 5590 5728 5797 5868 6073
 6140 6209 6277 6414 6551]
noise peaks indices
[]

In [19]:
first = qrs_detector.qrs_peaks_indices[0]
someLater = qrs_detector.qrs_peaks_indices[20]


Out[19]:
71

Small array


In [13]:
small = npArray[first:someLater]
sha = np.arange(small.shape[0])

In [14]:
plt.plot(small)
plt.show()



In [15]:
test = np.stack((sha, np.nan_to_num(small))).T
test


Out[15]:
array([[  0.00000000e+00,   9.21259843e-01],
       [  1.00000000e+00,   7.87401575e-01],
       [  2.00000000e+00,   6.22047244e-01],
       ..., 
       [  1.02200000e+03,   9.92125984e-01],
       [  1.02300000e+03,   8.89763780e-01],
       [  1.02400000e+03,   7.48031496e-01]])

In [16]:
ecg_data_path = 'ecg_data/small.csv'
np.savetxt(ecg_data_path, test, delimiter=',')
qrs_detector = QRSDetectorOffline(ecg_data_path=ecg_data_path, verbose=True,log_data=True, plot_data=True, 
                                  show_plot=True,signal_frequency=fs)


qrs peaks indices
[  71  137  205  341  410  478  546  617  682  751  819  890  957 1022]
noise peaks indices
[]

In [17]:
first = qrs_detector.qrs_peaks_indices[2]
someLater = qrs_detector.qrs_peaks_indices[6]
small = np.delete(qrs_detector.ecg_data_detected[first:someLater],0,1)
plt.plot(small)
plt.show()



In [ ]: